package com.yanqu.road.server.timer;

import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.rank.CrossRankMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.ConfigHelper;
import com.yanqu.road.utils.executor.GeneralTask;

import java.util.List;

/**
 * @author ：zfr
 * @date ：2021/9/15 18:19
 */
public class CrossRankUploadNotifyTask extends GeneralTask {
    private boolean isRun;

    @Override
    public void doRun() {
        try {
            if (!isRun) {
                isRun = true;
                int channelId = ConfigHelper.getInt("channelId");
                List<ServerInfo> openServerList = ServerListMgr.getOpenServerList();
                RankProto.CrossNotifyUploadRankDataMsg.Builder reqMsg = RankProto.CrossNotifyUploadRankDataMsg.newBuilder();
                YanQuMessage message = YanQuMessageUtils.buildMessage(GameProtocol.S_CROSS_NOTIFY_UPLOAD_RANK_DATA, reqMsg);
                for (ServerInfo serverInfo : openServerList) {
                    if (serverInfo.getChannelId() == channelId && !CrossRankMgr.isUploadOver(serverInfo.getServerId())) {
                        MessageHelper.sendPacket(serverInfo.getServerId(), 0, message);
                    }
                }
            }
        } catch (Exception e) {
            CrossRankMgr.getLogger().error("notify upload rank error", e);
        } finally {
            isRun = false;
        }

    }
}